<?php
$tblname = $_REQUEST["tblname"];
if ($tblname == "")
    $tblname = "tbl_pet";
?>

<script type="text/javascript">
    $(document).ready(function() {

        $('#petition').dataTable({
            "aaSorting": []
        });

        $("#tblname").change(function() {
            //alert($(this).val());
            window.location.href = "index.php?pages=pet_verify_approved&tblname=" + $(this).val();
        });

    });


    function verifyPetition(id) {
        var task = "verify";
//        $.post('pet_status_process.php', {task: task, id: id}, function(data) {
//            window.location.href = "index.php?pages=pet_verify_approved";
//        });

        var tblname = "<?= $tblname ?>";
        $('<div>').dialog2({
            autoOpen: true,
            title: "ตรวจสอบ",
            content: "dlg_verify_pet.php?id=" + id + "&task=" + task + "&tblname=" + tblname,
            id: "modal-date"
        });

    }

//    function cancelPetition(id) {
//        var task = "cancel";
//        //alert(id);
//
//        $('<div>').dialog2({
//            autoOpen: true,
//            title: "ยกเลิกฎีกา",
//            content: "dlg_cancel.php?pet_id=" + pet_id,
//            id: "modal-cancel"
//        });
//
//    }

    function cancel(id) {
        if (confirm("คุณแน่ใจใช่ไหมที่ต้องการ ยกเลิก สัญญานี้")) {
            var tblname = "<?= $tblname ?>";
            $('<div>').dialog2({
                autoOpen: true,
                title: "ยกเลิกฎีกา",
                content: "dlg_cancel.php?tblname=" + tblname + "&id=" + id,
                id: "modal-cancel"
            });
        }
    }

    function approvedPetition(id) {
        var task = "approved";
        var tblname = "<?= $tblname ?>";

        $('<div>').dialog2({
            autoOpen: true,
            title: "อนุมัติ",
            content: "dlg_date_approved_utility.php?id=" + id + "&task=" + task + "&tblname=" + tblname,
            id: "modal-date"
        });


    }
    function disapprovalPetition(id) {
        var task = "disapproval";
        var tblname = "<?= $tblname ?>";

        $.post('pet_status_process.php', {task: task, id: id}, function(data) {
            window.location.href = "index.php?pages=pet_verify_approved&tblname=" + tblname;
        });

    }

</script>

<?php
switch ($tblname) {
    case "tbl_pet":

        $sql = "SELECT DISTINCT tbl_pet.*, expenses1_name, expenses2_name, panngan2_name, panngan3_name, vender_name  "
                . " FROM  expenses_lvl1, expenses_lvl2, panngan2, panngan3, v_vender, tbl_pet "
                . " LEFT JOIN expenses_lvl3 ON expenses_lvl3.expenses3_id = ex3_id "
                . " WHERE confirm = 'ยืนยัน' "
                . " AND expenses_lvl1.expenses1_id = ex1_id "
                . " AND expenses_lvl2.expenses2_id = ex2_id "
                . " AND panngan2.panngan2_id = tbl_pet.panngan2_id "
                . " AND panngan3.panngan3_id = tbl_pet.panngan3_id "
                . " AND tbl_pet.receiver = v_vender.vender_id "
                . " ORDER BY tbl_pet.id DESC ";

        break;
    case "tbl_petout":
        $sql = "SELECT DISTINCT tbl_petout.*, panngan2_name, panngan3_name "
                . " , money_out2_name, money_out3_name, vender_name "
                . " FROM panngan2, panngan3, money_out2, tbl_petout "
                . " LEFT JOIN money_out3 ON tbl_petout.money_out3_id = money_out3.money_out3_id "
                . " LEFT JOIN v_vender ON tbl_petout.vender_id = v_vender.vender_id "
                . " WHERE confirm = 'ยืนยัน' "
                . " AND tbl_petout.panngan2_id = panngan2.panngan2_id "
                . " AND tbl_petout.panngan3_id = panngan3.panngan3_id "
                . " AND tbl_petout.money_out2_id = money_out2.money_out2_id "
                . " ORDER BY tbl_petout.id DESC";

        break;
}
$db = new myDBS();

//echo $sql;
$result = $db->runSQL($sql);
?>
<div class="control-group">
    <div class="controls-row">
        <select id="tblname" name="tblname" >
            <option value="">เลือก...</option>
            <option value="tbl_pet" <?php echo ($tblname == "tbl_pet") ? "selected" : ""; ?>>ฎีกาเงินในงบประมาณ</option>
            <option value="tbl_petout" <?php echo ($tblname == "tbl_petout") ? "selected" : ""; ?>>ฎีกาเงินนอกงบประมาณ</option>
        </select>
    </div>
</div>

<?php
if ($tblname == "tbl_pet") {
    ?>
    <table id="petition" class="table table-bordered table-hover" width="200%" >
        <caption><br></caption>
        <thead>
            <tr class="text-center">
                <th>#</th>
                <th>วันที่</th>
                <th>เลขที่ฎีกา</th>
                <th>เลขที่คลังรับ</th>
                <th>รหัสบัญชี</th>
                <th>แผนงาน</th>
                <th>งาน</th>
                <th>หมวด</th>
                <th>ประเภท</th>
                <th>ผู้รับเงิน</th>
                <th>จำนวนเงิน</th>
                <th>ภาษีมูลค่าเพิ่ม</th>
                <th>ภาษีหัก ณ ที่จ่าย</th>
                <th>จำนวนเงินสุทธิ</th>
                <th>ดู</th>
                <th>ลงรับฎีกา</th>
                <th>อนุมัติ</th>
            </tr>
        </thead>
        <tbody>
        <!--<p class='text-center'></p>-->
            <?php
            $i = 0;
            while ($rows = $db->fetch_object($result)) {
                $i++;
//            <td>$rows->panngan2_name</td>
//                        <td>$rows->panngan3_name</td>
                echo ($rows->status == "ยกเลิก") ? "<tr class='error'>" : "<tr>";
                //<td><p class='text-center'>$rows->pet_id</p></td>
                echo "
                        <td><p class='text-center'>$i</p></td>
                        <td><p class='text-center'>" . $rows->date_create . "</p></td>
                        <td><p class='text-center'>$rows->pet_id</p></td>
                        <td><p class='text-center'>$rows->finance_id</p></td>
                        <td>&nbsp;</td>
                        <td>$rows->panngan2_name</td>
                        <td>$rows->panngan3_name</td>
                        <td>$rows->expenses1_name</td>
                        <td>$rows->expenses2_name</td>
                        <td>$rows->vender_name</td>
                        <td><p class='text-right'>" . number_format($rows->amount, 2) . "</p></td>";
                echo "<td><p class='text-right'>" . number_format($rows->tt, 2) . "</p></td>"
                . "<td><p class='text-right'>" . number_format($rows->tt1, 2) . "</p></td>"
                . "<td><p class='text-right'>" . number_format($rows->net, 2) . "</p></td>";
                echo "<td><p class='text-center'><a href='pet_print.php?id=" . $rows->id . "' target='_blank'><i class='icon-eye-open'></i></a></p></td>";
                echo "<td><p class='text-center'>";
                if ($rows->verify_status == "รอตรวจสอบ" && $rows->status == "") {

                    echo "<a href='javascript:verifyPetition(\"$rows->id\")' data-toggle='tooltip' title='ผ่าน'><i class='icon-ok'></i></a>&nbsp;<big>|</big>&nbsp;<a href='javascript:cancel(\"$rows->id\")' data-toggle='tooltip' title='ยกเลิก'><i class='icon-remove'></i></a>";
                } elseif ($rows->status == "ยกเลิก") {
                    echo $rows->status;
                } else {
                    echo $rows->verify_status;
                }
                echo "</p></td>";
                echo "<td><p class='text-center'>	 ";
                if ($rows->verify_status == "ผ่าน") {
                    if ($rows->approved_status == "รออนุมัติ") {
                        echo "<a href='javascript:approvedPetition(\"$rows->id\")' data-toggle='tooltip' title='อนุมัติ'><i class='icon-ok'></i></a>&nbsp;<big>|</big>&nbsp;<a href='javascript:disapprovalPetition(\"$rows->id\")' data-toggle='tooltip' title='ไม่อนุมัติ'><i class='icon-remove'></i></a>";
                    } else {
                        echo $rows->approved_status;
                    }
                } elseif ($rows->status == "ยกเลิก") {
                    echo "ยกเลิก";
                } else {
                    echo "รอการตรวจสอบ";
                }
                echo "</p></td>";
                echo "</tr>";
            }
            ?>
        </tbody>
    </table>
    <?php
} elseif ($tblname == "tbl_petout") {
    ?>

    <table id="petition" class="table table-bordered" width="150%" >
        <caption><br></caption>
        <thead>
            <tr>
                <th>#</th>
                <th>เลขที่ฎีกา</th>
                <th>เลขที่ฎีกาคลัง</th>
                <th>วันที่</th>
                <th>หมวด</th>
                <th>ประเภท</th>
                <th>จำนวนเงิน</th>
                <th>ชื่อผู้รับเงิน</th>
                <th>ดู</th>
                <th>ลงรับฎีกา</th>
                <th>อนุมัติ</th>
            </tr>
        </thead>
        <tbody>	
            <?php
            $i = 0;
            while ($rows = $db->fetch_object($result)) {
                $i++;
                ?>
                <tr class="<?php
                if ($rows->status == "ยกเลิก") {
                    echo " error";
                } else if ($rows->confirm == "ยืนยัน") {
                    //echo " success";
                }
                ?>">
                    <td><p class="text-center"><?php echo $i; ?></p></td>
                    <td><p class="text-center"><?php echo $rows->petout_id; ?></p></td>
                    <td><p class="text-center"><?php echo $rows->finance_id; ?></p></td>
                    <td><p class="text-center"><?php echo date_format(date_create($rows->date_create), "d/m/Y"); ?></p></td>
                    <td><?php echo $rows->money_out2_name; ?></td>
                    <td><?php echo $rows->money_out3_name; ?></td>
                    <td><p class="text-right"><?php echo number_format($rows->amount, 2); ?></p></td>
                    <td><p class="text-left"><?php echo $rows->vender_name; ?></p></td>
                        <?php
                        echo "<td><p class='text-center'><a href='petout_print.php?id=" . $rows->id . "' target='_blank'><i class='icon-eye-open'></i></a></p></td>";
                        echo "<td><p class='text-center'>";
                        if ($rows->verify_status == "รอตรวจสอบ" && $rows->status == "") {

                            echo "<a href='javascript:verifyPetition(\"$rows->id\")' data-toggle='tooltip' title='ผ่าน'><i class='icon-ok'></i></a>&nbsp;<big>|</big>&nbsp;<a href='javascript:cancel(\"$rows->id\")' data-toggle='tooltip' title='ยกเลิก'><i class='icon-remove'></i></a>";
                        } elseif ($rows->status == "ยกเลิก") {
                            echo $rows->status;
                        } else {
                            echo $rows->verify_status;
                        }
                        echo "</p></td>";
                        echo "<td><p class='text-center'>	 ";
                        if ($rows->verify_status == "ผ่าน") {
                            if ($rows->approved_status == "รออนุมัติ") {
                                echo "<a href='javascript:approvedPetition(\"$rows->id\")' data-toggle='tooltip' title='อนุมัติ'><i class='icon-ok'></i></a>&nbsp;<big>|</big>&nbsp;<a href='javascript:disapprovalPetition(\"$rows->id\")' data-toggle='tooltip' title='ไม่อนุมัติ'><i class='icon-remove'></i></a>";
                            } else {
                                echo $rows->approved_status;
                            }
                        } elseif ($rows->status == "ยกเลิก") {
                            echo "ยกเลิก";
                        } else {
                            echo "รอการตรวจสอบ";
                        }
                        echo "</p></td>";
                        ?>
                </tr>
                <?php
            }
            ?>

            <?php
        }
        ?>
